package com.digital.smartgrow.controller.upscale;

import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.common.util.WebTool;
import com.digital.smartgrow.dto.upscale.UpPlanPageDTO;
import com.digital.smartgrow.entity.datacenter.Teacher;
import com.digital.smartgrow.entity.upscale.UpPlan;
import com.digital.smartgrow.service.upscale.UpPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 晋升规划 前端控制器
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-12
 */
@Api(tags = "晋升规划")
@RestController
@RequestMapping("${context.prefix}/syc/upPlan")
public class UpPlanController {

    @Autowired
    private UpPlanService upPlanService;

    @ApiOperation(value = "晋升规划-分页查询")
    @PostMapping("/list")
    public Object getListByPage(@RequestBody UpPlanPageDTO upPlanPageDTO){
        return upPlanService.getListByPage(upPlanPageDTO);
    }


    @ApiOperation(value = "晋升规划-关键字查询")
    @PostMapping("/key")
    public Object getListByKey( @RequestParam(required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam(required = false,defaultValue = "15")Integer pageSize ,
                                @RequestParam(required = false)String keyword){
        return upPlanService.getListByKey(keyword,pageNum,pageSize);
    }

    @ApiOperation(value = "晋升规划-详情")
    @GetMapping("/{id}")
    public Object getInfo(@PathVariable Integer id){
        if (id == null) return Result.fail("id不能为空");
        return upPlanService.getInfo(id);
    }

    @ApiOperation(value = "晋升规划-删除")
    @DeleteMapping("/{id}")
    public Object removeById(@PathVariable Integer id){
      if (id == null) return Result.fail("id不能为空");
      return upPlanService.removeById(id);
    }
    @ApiOperation(value = "晋升规划-新增")
    @PostMapping()
    public Object add( @RequestBody UpPlan upPlan){
        Teacher currentUser = WebTool.getCurrentUser();
        upPlan.setCode(currentUser.getEmployeeId());
        upPlan.setName(currentUser.getName());
        upPlan.setSex(currentUser.getGender());
        return upPlanService.save(upPlan);
    }

    @ApiOperation(value = "晋升规划-编辑")
    @PutMapping()
    public Object edit(@RequestBody UpPlan upPlan){
    if (upPlan==null||upPlan.getId() == null)
       return Result.fail("id不能为空");
       return upPlanService.updateById(upPlan);
    }
}
